package jbl.service.impl;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jbl.dao.IconClsDaoI;
import jbl.model.Code_iconCls;
import jbl.pageModel.DataGrid;
import jbl.pageModel.IconCls;
import jbl.service.IconClsServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("iconClsService")
public class IconClsServiceImpl implements IconClsServiceI{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(IconClsServiceImpl.class);
	
	private IconClsDaoI iconClsDao;
	
	@Autowired
	public void setIconClsDao(IconClsDaoI iconClsDao) {
		this.iconClsDao = iconClsDao;
	}

	@Override
	public DataGrid dataGrid(IconCls iconCls) {
			// TODO Auto-generated method stub
			DataGrid dg = new DataGrid();
			try {
				List<IconCls> daList = new ArrayList<IconCls>();
				String hql = " from Code_iconCls t where 1=1 ";
				String totalHql = "select count(*) " + hql;
				hql = addOrder(hql, iconCls);
				List<Code_iconCls> list = iconClsDao.find(hql, iconCls.getPage(), iconCls.getRows());
				if(null != list && list.size() > 0){
					for(Code_iconCls t : list){
						IconCls iconCls2 = new IconCls();
						BeanUtils.copyProperties(t, iconCls2);
						daList.add(iconCls2);
					}
				}
				
				dg.setRows(daList);
				dg.setTotal(iconClsDao.count(totalHql));
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				logger.error("查询iconCls信息error:"+e.getMessage());
			}
			return dg;
	}
	
	private String addOrder(String hql , IconCls iconCls){
		if(null != iconCls.getSort()){
			hql += " order by t."+iconCls.getSort() +" " + iconCls.getOrder();
		}
		return hql;
	}

	@Override
	public Json add(IconCls iconCls) {
		Json j = new Json();
		try {
			Code_iconCls cls = new Code_iconCls();
			BeanUtils.copyProperties(iconCls, cls);
			cls.setId(Encrypt.createUUID());
			cls.setCjsj(new Timestamp(new Date().getTime()));
			
			iconClsDao.save(cls);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			j.setMsg("添加失败");
			logger.error("添加改iconCls信息error:"+e.getMessage());
		}
		return j;
	}

	@Override
	public Json delete(String ids) {
		Json j = new Json();
		try{
			if(null != ids){
				for(String id : ids.split(",")){
					if(!"".equals(id.trim())){
						Code_iconCls tda = iconClsDao.get(Code_iconCls.class, id);
						iconClsDao.delete(tda);
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			j.setMsg("删除失败");
			logger.error("删除iconCls信息error:"+e.getMessage());
		}
		return j;
	}

	@Override
	public Json edit(IconCls iconCls) {
		Json j = new Json();
		try {
			Code_iconCls cls = new Code_iconCls();
			BeanUtils.copyProperties(iconCls, cls);
			iconClsDao.update(cls);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			j.setMsg("修改失败");
			logger.error("修改iconCls信息error:"+e.getMessage());
		}
		return j;
	}

	@Override
	public List<IconCls> comboboxInfo() {
		List<IconCls> daList = new ArrayList<IconCls>();
		try {
			String hql = " from Code_iconCls t order by t.cjsj asc ";
			List<Code_iconCls> list = iconClsDao.find(hql);
			if(null != list && list.size() > 0){
				for(Code_iconCls t : list){
					IconCls iconCls2 = new IconCls();
					BeanUtils.copyProperties(t, iconCls2);
					daList.add(iconCls2);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("查询iconCls信息error:"+e.getMessage());
		}
		return daList;
	}
	
	
}
